<template>
	<view class="overlay" v-if="showOverlay">
		<view class="popup">
			<text>是否授权手机号？</text>
			<button @click="onConfirm">确定</button>
			<button @click="onCancel">取消</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showOverlay: true
			};
		},
		methods: {
			onConfirm() {
				this.showOverlay = false;
				this.getPhoneNumber();
			},
			onCancel() {
				this.showOverlay = false;
			},
			getPhoneNumber() {
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						if (loginRes.code) {
							uni.request({
								url: 'https://your-api-url.com/getPhoneNumber',
								method: 'POST',
								data: {
									code: loginRes.code
								},
								success: (res) => {
									if (res.data.code === 200) {
										const phoneNumber = res.data.phoneNumber;
										uni.setStorageSync('phoneNumber', phoneNumber);
										uni.showToast({
											title: '手机号绑定成功',
											icon: 'success'
										});
									} else {
										uni.showToast({
											title: '获取手机号失败，请稍后重试',
											icon: 'none'
										});
									}
								},
								fail: (err) => {
									console.error('请求接口失败：', err);
									uni.showToast({
										title: '网络请求失败，请检查网络',
										icon: 'none'
									});
								}
							});
						} else {
							console.error('获取登录凭证失败：', loginRes.errMsg);
							uni.showToast({
								title: '获取登录凭证失败，请稍后重试',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						console.error('登录失败：', err);
						uni.showToast({
							title: '登录失败，请检查网络',
							icon: 'none'
						});
					}
				});
			}
		}
	};
</script>

<style>
	.overlay {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.popup {
		background: #fff;
		padding: 20rpx;
		border-radius: 10rpx;
		text-align: center;
	}

	button {
		margin: 10rpx;
	}
</style>